#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
import unittest

import cnode.test.fakeLibvirt as libvirt

from cnode.libvirtXmlObject import *

from cnode.test.testConstant import GUEST_XML, INTERFACE_XML,DISK_XML
from cvirt.common.test import BaseTest

class TestLibvirtDomain(BaseTest):
    def setUp(self):
        super(TestLibvirtDomain, self).setUp()

        self.host = "127.0.0.1"
       
        libvirt_uri = 'qemu:///system'
        auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
                        'root',
                        None]

        self._conn = libvirt.openAuth(libvirt_uri, auth, 0)

    
    def tearDown(self):
        super(TestLibvirtDomain, self).tearDown()


    def testGenCaps(self):
        xml = self._conn.getCapabilities()
        caps = Caps()
        caps.parse_xml(xml)
        cpu = caps.host.cpu

        self.assertEqual(caps.host.uuid,'cef19ce0-0ca2-11df-855d-b19fbce37686')
        self.assertEqual(cpu.arch,'x86_64')
        self.assertEqual(cpu.model,'Penryn')
        self.assertEqual(cpu.vendor,'Intel')
        self.assertEqual(cpu.sockets,1)
        self.assertEqual(cpu.cores,2)
        self.assertEqual(cpu.threads,1)



    def testGuestDisk(self):
        xml = DISK_XML
        disks = GuestDiskList()
        disks.parse_xml(xml)

        for d in disks.disks:
            self.assertEqual(d.source_type,'file')
            self.assertEqual(d.driver_name,'qemu')
            if d.source_device == 'cdrom':
                self.assertEqual(d.driver_format,'raw')
                self.assertEqual(d.source_path,'/iso/iso/ubuntu-12.04-alternate-i386.iso')
            elif d.source_device == 'floppy':
                self.assertEqual(d.driver_format,'raw')
                self.assertEqual(d.source_path,'/etc/thor/virtio-win-1.1.16.vfd')

    def testGuestInterface(self):
        xml = INTERFACE_XML
        interface = GuestInterface()
        interface.parse_xml(xml)
        self.assertEqual(interface.net_type,'direct')
        self.assertEqual(interface.source_dev,'eth0')
        self.assertEqual(interface.mac_addr,'52:54:00:d7:4b:43')

    def testParseGuest(self):
        guest = Guest()
        guest.parse_xml(GUEST_XML)

        self.assertEqual(guest.type,'kvm')
        self.assertEqual(guest.vcpus,2)
        self.assertEqual(guest.memory,512L)
        self.assertEqual(guest.name,'i-00000001')
        self.assertEqual(guest.sysinfo.bios_vendor,'morningcloud')
        self.assertEqual(guest.sysinfo.bios_version,'1.0')

        self.assertEqual(guest.clock.offset,'local')
        #sound
        self.assertEqual(guest.sound.model,'ac97')
        # graphic
        self.assertEqual(guest.graphic.type,'spice')
        self.assertEqual(guest.graphic.listen,'0.0.0.0')
        self.assertEqual(guest.graphic.autoport,False)


        print guest.to_xml()


    def testGenerateGuest(self):

        guest = Guest()
        guest.type = 'kvm'
        guest.name = 'i-00000001'

        guest.memory = 512 * 1024
        guest.vcpus = 2

        cpu = GuestCPU()
        cpu.mode = 'host-model'

        guest.cpu = cpu

        guest.os_type = 'hvm'
        guest.qemu_command='-snapshot'

        sysinfo = GuestSysinfo(system_product='Tcloud', system_version='2.2', system_serial='123456',
                               system_uuid='225555')


        guest.sysinfo = sysinfo
        guest.os_smbios = GuestSMBIOS()

        guest.bootorder = ["hd","iso"]
        guest.acpi = True
        guest.apic = True


        clk = GuestClock()
        clk.offset = "local"
        guest.set_clock(clk)

        disk = GuestDisk()
        disk.driver_name = 'qemu'
        disk.driver_format = 'qcow2'
        disk.driver_cache = 'none'
        disk.source_path = '/home/ezzze/instances/i-00000001/disk'
        disk.target_dev = 'vda'

        disk.target_bus='virtio'
        guest.add_device(disk)

        cdrom = GuestDisk()
        cdrom.source_device = 'cdrom'
        cdrom.driver_name = 'qemu'
        cdrom.driver_format = 'raw'

        cdrom.source_path = '/home/ezzze/instances/i-00000001/disk'
        cdrom.target_dev = 'vdc'

        cdrom.target_bus='ide'

        guest.add_device(cdrom)

        interface = GuestInterface()
        interface.net_type='bridge'
        interface.mac_addr ='123123'
        interface.model='virtio'
        interface.source_dev='br0'

        guest.add_device(interface)
        need_usb_tablet = False


        tablet = GuestInput()
        tablet.type = "tablet"
        tablet.bus = "usb"

        mouse = GuestInputMouse()
        mouse.type='mouse'
        mouse.bus='ps2'

        guest.add_device(tablet)

        # if spice:
        channel = Channel()
        channel.target_name = "com.redhat.spice.0"
        guest.add_device(channel)

        graphics = GuestGraphics()
        graphics.type = "spice"
        graphics.port = "5269"
        graphics.listen = '0.0.0.0'
        guest.add_device(graphics)
        guest.add_device(GraphicVideo())


        usb_redir = RedirDevice()

        guest.add_device(usb_redir)

        self.assertEqual(guest.to_xml(),
'''<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" type="kvm">
  <name>i-00000001</name>
  <memory>524288</memory>
  <vcpu>2</vcpu>
  <sysinfo type="smbios">
    <bios>
      <entry name="vendor">morningcloud</entry>
      <entry name="version">1.0</entry>
    </bios>
    <system>
      <entry name="manufacturer">Tcloud</entry>
      <entry name="product">Tcloud</entry>
      <entry name="version">2.2</entry>
      <entry name="serial">123456</entry>
      <entry name="uuid">225555</entry>
    </system>
  </sysinfo>
  <os>
    <type>hvm</type>
    <boot dev="hd"/>
    <boot dev="iso"/>
    <smbios mode="sysinfo"/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <clock offset="local"/>
  <cpu mode="host-model" match="exact"/>
  <devices>
    <disk type="file" device="disk">
      <driver name="qemu" type="qcow2" cache="none"/>
      <source file="/home/ezzze/instances/i-00000001/disk"/>
      <target bus="virtio" dev="vda"/>
    </disk>
    <disk type="file" device="cdrom">
      <driver name="qemu" type="raw"/>
      <source file="/home/ezzze/instances/i-00000001/disk"/>
      <target bus="ide" dev="vdc"/>
    </disk>
    <interface type="bridge">
      <mac address="123123"/>
      <model type="virtio"/>
      <source bridge="br0"/>
    </interface>
    <input type="tablet" bus="usb"/>
    <channel type="spicevmc">
      <target type="virtio" name="com.redhat.spice.0"/>
    </channel>
    <graphics type="spice" autoport="no" listen="0.0.0.0" port="5269">
      <image compression="auto_glz"/>
      <jpeg compression="always"/>
      <streaming mode="all"/>
    </graphics>
    <video>
      <model heads="1" vram="65536" ram="65536" type="qxl"/>
    </video>
    <redirdev type="spicemvc" bus="usb"/>
  </devices>
  <qemu:commandline>
    <qemu:arg value="-snapshot"/>
  </qemu:commandline>
</domain>
''')


